<?php

namespace Miniapp\Controllers;

class RedpacketController extends AuthController {

    /**
     * 添加视频
     */
    public function AddVideoAction() {
        $videoUrl = $_POST['videoUrl'];
        $userId = $this->userId;
        $money = $_POST['money'];
        $type = 0;
        $greeting = $_POST['greeting'];
        $result = service("Video/VideoService")->AddVideo($userId, $videoUrl, $money, $type, $img, $greeting);
        $result['user_img'] = $this->user['img'];
        $this->renderJson($result);
    }

    /**
     * 视频详情
     */
    public function videoDetailAction() {
        $id = $_POST['id'];
        $videoService = service("Video/VideoService");
        $result = $videoService->getVideoInfoById($id);
        if (empty($result)) {
            E("视频不存在");
        }
        if ($result['isDel'] == IS_DEL_YES) {
            E("视频不存在");
        }
        $begRedpacketList = service("Pay/PayService")->getVideoBegRedpacketList($result['id']);
        if ($result['userId'] != $this->userId && !empty($this->userId)) {
            $videoService->addVideoViewLog($result['id'], $this->userId);
            $result['viewCount'] = $result['viewCount'] + 1;
        }
        $result['user_img'] = $this->user['img'];
        $result['begList'] = $begRedpacketList ? $begRedpacketList : array();
        $result['userId'] = lib("Encrypt")->encrypt($result['userId']);
        $this->renderJson($result);
    }

    /**
     * 视频讨取红包列表记录
     */
    public function videoBegRedpacketListAction() {
        $p = intval($_POST['p']) ? intval($_POST['p']) : 1;
        $psize = intval($_POST['psize']) ? intval($_POST['psize']) : 10;
        $id = intval($_POST['id']) ? intval($_POST['id']) : E("请输入视频ID");
        $result = service("Pay/PayService")->getVideoBegRedpacketList($id, $p, $psize);
        $this->renderJson($result);
    }

    /**
     * 我的视频列表
     */
    public function myVideoListAction() {
        $p = intval($_POST['p']) ? intval($_POST['p']) : 1;
        $psize = intval($_POST['psize']) ? intval($_POST['psize']) : 10;
        $videoService = service("Video/VideoService");
        $result['list'] = $videoService->getMyVideoList($this->userId, $p, $psize);
        $result['minibegMoney'] = $this->user['minibegMoney'];
        $result['minibegCount'] = $this->user['minibegCount'];
        $this->renderJson($result);
    }

    /**
     * 保存
     */
    public function saveQmzfOpenIdAction() {
        $qmzfOpenId = $_POST['qmzfOpenId'];
        $userService = service("User/UserService");
        $result = $userService->saveQmzfOpenId($this->userId, $qmzfOpenId);
        $this->renderJson($result);
    }

}
